﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using PMSA.Framework.Database;

/// <summary>
/// Summary description for COrderFacade
/// </summary>
public class COrderFacade
{
    private OrderDAL db = new OrderDAL();
    public IList<Order> List(int pageIndex, int pageMax, int userid, int isView, int isFinish, int isCancel, DateTime fromDate, DateTime toDate)
    {
        IList<Order> list = null;
        DataSet ds = new DataSet();
        ds = db.List(pageIndex, pageMax, userid, isView, isFinish, isCancel, fromDate, toDate);
        if (ds != null && ds.Tables.Count > 0)
        {
            list = CDb.MapList<Order>(ds.Tables[0]);
        }

        return list;
    }

    public string Cancel(int oi)
    {
        return db.Cancel(oi);
    }

    public string Finish(int oi)
    {
        return db.Finish(oi);
    }

    public string Insert(Order order)
    {
        int ret = 0;
        int orderid = db.Insert(order.UserId, order.OrderCustomerName, order.OrderCustomerNote, order.OrderCustomerPhone, order.OrderCustomerAddress, order.OrderCustomerEmail);
        
        foreach (OrderDetail item in order.OrderDetails)
        {
            item.OrderId = orderid;
            ret = InsertDetail(item);
        }
        return ret.ToString();
    }
    public int InsertDetail(OrderDetail detail)
    {
        int id = db.InsertDetail(detail.OrderId, detail.ProductId, detail.Quantity, detail.PriceAtBuy);
        return id;
    }

    public Order Select(int orderid)
    {
        Order obj = new Order();
        DataSet ds = new DataSet();
        ds = db.Select(orderid);
        if (ds != null && ds.Tables.Count > 0)
        {
            obj = CDb.Map<Order>(ds.Tables[0]);
        }
        return obj;
    }
    public IList<OrderDetail> SelectDetail(int orderid)
    {
        IList<OrderDetail> obj = null;
        DataSet ds = new DataSet();
        ds = db.SelectDetail(orderid);
        if (ds != null && ds.Tables.Count > 0)
        {
            obj = CDb.MapList<OrderDetail>(ds.Tables[0]);
        }
        return obj;
    }
}